package com.xc.entity.query;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;

@Data
public class PageResult<T> extends PageQuery {
    @Schema(title = "总记录数")
    private Long recordsSum;

    @Schema(title = "当前页数据")
    private T list;

    public static PageResultBuilder convertBuilder() {
        return new PageResultBuilder();
    }

    public static class PageResultBuilder<M, O> {
        private M dataSrc;
        private Class<O> oupCla;

        public PageResultBuilder dataSrc(M dataSrc) {
            this.dataSrc = dataSrc;
            return this;
        }

        public PageResultBuilder to(Class oupCla) {
            this.oupCla = oupCla;
            return this;
        }

        public PageResult build() {
            if (dataSrc == null) {
                return new PageResult();
            }
            List<O> oupList = BeanUtil.copyToList(((IPage) dataSrc).getRecords(), oupCla);
            PageResult<List<O>> result = new PageResult<>();// 这里实际是集合<O>，测试是否要改
            result.setPageNo(((IPage) dataSrc).getCurrent());
            result.setPageSize(((IPage) dataSrc).getSize());
            result.setRecordsSum(((IPage) dataSrc).getTotal());
            result.setList(oupList);
            return result;
        }
    }
}
